﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Input;

namespace EnsureWeb.Controls
{
    public class Pager : StackPanel
    {
        public int PageCount { get; set; }
        public int PageSize { get; set; }
        public Action PageIndexChange { get; set; }

        public long ItemCount
        {
            set
            {
                this.PageCount = Convert.ToInt32(Math.Ceiling(1d * value / this.PageSize));

                this.itemCountTxt.Text = string.Format("Total: {0}", value);
            }
        }

        public int PageIndex
        {
            get
            {
                return this.pageIndex;
            }
            set
            {
                this.pageIndex = value;

                this.pageTxt.Text = string.Format(pageFormat, pageIndex + 1, PageCount);

                if (PageIndexChange != null)
                    PageIndexChange();
            }
        }

        public Pager()
            : base()
        {
            this.Orientation = Orientation.Horizontal;
            this.PageSize = 20;

            preIcon = new ImageButton();
            preIcon.Margin = new Thickness(2);
            preIcon.NormalImage = this.GetImage("arrow_left_normal");
            preIcon.HoverImage = this.GetImage("arrow_left_hover");
            preIcon.ClickImage = this.GetImage("arrow_left_click");
            preIcon.Click += new RoutedEventHandler(preIcon_Click);

            nextIcon = new ImageButton();
            nextIcon.Margin = new Thickness(2);
            nextIcon.NormalImage = this.GetImage("arrow_right_normal");
            nextIcon.HoverImage = this.GetImage("arrow_right_hover");
            nextIcon.ClickImage = this.GetImage("arrow_right_click");
            nextIcon.Click += new RoutedEventHandler(rightIcon_Click);

            firstIcon = new ImageButton();
            firstIcon.Margin = new Thickness(2);
            firstIcon.NormalImage = this.GetImage("arrow_left_normal2");
            firstIcon.HoverImage = this.GetImage("arrow_left_hover2");
            firstIcon.ClickImage = this.GetImage("arrow_left_click2");
            firstIcon.Click += new RoutedEventHandler(firstIcon_Click);

            lastIcon = new ImageButton();
            lastIcon.Margin = new Thickness(2);
            lastIcon.NormalImage = this.GetImage("arrow_right_normal2");
            lastIcon.HoverImage = this.GetImage("arrow_right_hover2");
            lastIcon.ClickImage = this.GetImage("arrow_right_click2");
            lastIcon.Click += new RoutedEventHandler(lastIcon_Click);

            pageTxt = new TextBlock();
            pageTxt.FontSize = 13;
            pageTxt.Margin = new Thickness(0, 2, 3, 2);

            pageInput = new TextBox();
            pageInput.Margin = new Thickness(2);
            pageInput.Width = 45;
            pageInput.Height = 20;
            pageInput.KeyDown += new KeyEventHandler(pageInput_KeyDown);

            turnButton = new HyperlinkButton();
            turnButton.Margin = new Thickness(-26, 2, 2, 2);
            turnButton.Background = new SolidColorBrush(Colors.White);
            turnButton.Content = "Go";
            turnButton.Width = 19;
            turnButton.VerticalAlignment = VerticalAlignment.Center;
            turnButton.Click += new RoutedEventHandler(turnButton_Click);

            itemCountTxt = new TextBlock();
            itemCountTxt.FontSize = 13;
            itemCountTxt.Margin = new Thickness(10, 2, 0, 2);
            itemCountTxt.VerticalAlignment = VerticalAlignment.Center;

            this.Children.Add(firstIcon);
            this.Children.Add(preIcon);
            this.Children.Add(pageTxt);
            this.Children.Add(nextIcon);
            this.Children.Add(lastIcon);

            this.Children.Add(pageInput);
            this.Children.Add(turnButton);

            this.Children.Add(itemCountTxt);
        }

        private ImageSource GetImage(string imageName)
        {
            string imageUrl = string.Format("/EnsureWeb.Controls;component/Images/{0}.png", imageName);
            return new BitmapImage(new Uri(imageUrl, UriKind.RelativeOrAbsolute));
        }

        private void preIcon_Click(object sender, RoutedEventArgs e)
        {
            if (pageIndex > 0)
                PageIndex = --pageIndex;
        }

        private void rightIcon_Click(object sender, RoutedEventArgs e)
        {
            if (pageIndex < PageCount - 1)
                PageIndex = ++pageIndex;
        }

        void firstIcon_Click(object sender, RoutedEventArgs e)
        {
            if (pageIndex > 0)
                PageIndex = 0;
        }

        void lastIcon_Click(object sender, RoutedEventArgs e)
        {
            if (pageIndex < PageCount - 1)
                PageIndex = PageCount - 1;
        }

        void turnButton_Click(object sender, RoutedEventArgs e)
        {
            int num = 0;
            if (int.TryParse(this.pageInput.Text, out num))
            {
                if (0 <= num && num <= PageCount)
                    PageIndex = num - 1;
            }

        }

        void pageInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
                return;

            int num = 0;
            if (int.TryParse(this.pageInput.Text, out num))
            {
                if (0 <= num && num <= PageCount)
                    PageIndex = num - 1;
            }
        }

        private ImageButton preIcon;
        private ImageButton nextIcon;
        private ImageButton firstIcon;
        private ImageButton lastIcon;
        private TextBox pageInput;
        private HyperlinkButton turnButton;
        private TextBlock pageTxt;
        private TextBlock itemCountTxt;
        private int pageIndex;
        private readonly string pageFormat = "{0}/{1}";
    }
}
